package light.mvc.service.wxn.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.sys.OrganizationBO;
import light.mvc.model.sys.UserBO;
import light.mvc.model.wxn.WeiMenuBO;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.wxn.WeiMenuVO;
import light.mvc.service.base.ServiceException;
import light.mvc.service.wxn.WeiMenuServiceI;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.JsonArray;

@Service
public class WeiMenuServiceImpl implements WeiMenuServiceI {

	@Autowired
	private BaseDaoI<WeiMenuBO> weiMenuDao;

	@Override
	public void add(WeiMenuVO r) {
		WeiMenuBO t = new WeiMenuBO();
		BeanUtils.copyProperties(r, t);
		if ((r.getPid() != null) && !"".equals(r.getPid())) {
			t.setWeiMenuBO(weiMenuDao.get(WeiMenuBO.class, r.getPid()));
		}		
		weiMenuDao.save(t);
	}

	@Override
	public void delete(Long id) {
		WeiMenuBO t = weiMenuDao.get(WeiMenuBO.class, id);
		del(t);
	}
	
	private void del(WeiMenuBO t) {
		if ((t.getWeiMenuBOs() != null) && (t.getWeiMenuBOs().size() > 0)) {
			for (WeiMenuBO r : t.getWeiMenuBOs()) {
				del(r);
			}
		}
		weiMenuDao.delete(t);
	}

	@Override
	public void edit(WeiMenuVO r) {
		WeiMenuBO t = weiMenuDao.get(WeiMenuBO.class, r.getId());
		t.setIsEnable(r.getIsEnable());
		t.setNote(r.getNote());
		t.setMenuType(r.getMenuType());
		t.setMenuName(r.getMenuName());
		t.setMenuEvent(r.getMenuEvent());
		t.setGotoUrl(r.getGotoUrl());
		t.setMenuSeq(r.getMenuSeq());
		if ((r.getPid() != null) && !"".equals(r.getPid())) {
			t.setWeiMenuBO(weiMenuDao.get(WeiMenuBO.class, r.getPid()));
		}
		weiMenuDao.update(t);
		
	}

	@Override
	public WeiMenuVO get(Long id) {
		WeiMenuBO t = weiMenuDao.get(WeiMenuBO.class, id);
		WeiMenuVO r = new WeiMenuVO();
		r.setId(id);
		r.setIsEnable(t.getIsEnable());
		r.setNote(t.getNote());
		r.setMenuType(t.getMenuType());
		r.setMenuName(t.getMenuName());
		r.setMenuEvent(t.getMenuEvent());
		r.setGotoUrl(t.getGotoUrl());
		r.setMenuSeq(t.getMenuSeq());
		if (t.getWeiMenuBO() != null) {
			r.setPid(t.getWeiMenuBO().getId());
		}
		return r;
	}

	@Override
	public List<WeiMenuVO> dataGrid(WeiMenuVO weiMenu, PageFilter ph) {
		List<WeiMenuVO> ul = new ArrayList<WeiMenuVO>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from WeiMenuBO t ";
		List<WeiMenuBO> l = weiMenuDao.find(hql + whereHql(weiMenu, params)+ orderHql(ph), params, ph.getPage(), ph.getRows());
		for (WeiMenuBO t : l) {
			WeiMenuVO u = new WeiMenuVO();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	}

	@Override
	public Long count(WeiMenuVO weiMenu, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from WeiMenuBO t ";
		return weiMenuDao.count(
				"select count(*) " + hql + whereHql(weiMenu, params), params);
	}

	private String whereHql(WeiMenuVO weiMenu, Map<String, Object> params) {
		String hql = "";
		if (weiMenu != null) {
			hql += " where 1=1 ";
			if (weiMenu.getId() != null) {
				hql += " and t.name like :name";
				params.put("name", "%%" + weiMenu.getId() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public List<WeiMenuVO> treeGrid() {
		List<WeiMenuVO> lr = new ArrayList<WeiMenuVO>();
		List<WeiMenuBO> l = weiMenuDao.find("from WeiMenuBO t left join fetch t.weiMenuBO  order by t.menuSeq");
		if ((l != null) && (l.size() > 0)) {
			for (WeiMenuBO t : l) {
				WeiMenuVO r = new WeiMenuVO();
				BeanUtils.copyProperties(t, r);
				if (t.getWeiMenuBO() != null) {
					r.setPid(t.getWeiMenuBO().getId());
				}
				lr.add(r);
			}
		}
		return lr;
	}
	
	@Override
	public JSONObject treeMenu() {
		List<WeiMenuVO> lr = new ArrayList<WeiMenuVO>();
		List<WeiMenuBO> l = weiMenuDao.find("from WeiMenuBO t left join fetch t.weiMenuBO where t.isEnable=1  order by t.menuSeq");
				
		if ((l != null) && (l.size() > 0)) {
			for (WeiMenuBO t : l) {
				WeiMenuVO r = new WeiMenuVO();
				BeanUtils.copyProperties(t, r);
				if (t.getWeiMenuBO() != null) {
					r.setPid(t.getWeiMenuBO().getId());
				}
				lr.add(r);
			}
		}
		JSONObject resultJson = new JSONObject();
		JSONArray parentArray = new JSONArray();
		
		for (WeiMenuVO weiMenuVO : lr) {
			JSONArray childArray=new JSONArray();
			if(weiMenuVO.getPid()==null){
				JSONObject parentJson = new JSONObject();
				Long pid=weiMenuVO.getId();
				for(WeiMenuVO childweiMenuVO : lr){
					JSONObject childJson = new JSONObject();
					if(pid==childweiMenuVO.getPid()){
						childJson.accumulate("name",childweiMenuVO.getMenuName());
						if(childweiMenuVO.getMenuType()==1){
							childJson.accumulate("type","click");
							childJson.accumulate("key",childweiMenuVO.getMenuEvent());
						}else{
							childJson.accumulate("type","view");
							childJson.accumulate("url",childweiMenuVO.getGotoUrl());
						}
						childArray.put(childJson);
					}
				}
				parentJson.accumulate("name",weiMenuVO.getMenuName());
				if(childArray.length()==0){
					if(weiMenuVO.getMenuType()==1){
						parentJson.accumulate("type","click");
						parentJson.accumulate("key",weiMenuVO.getMenuEvent());
					}else{
						parentJson.accumulate("type","view");
						parentJson.accumulate("url",weiMenuVO.getGotoUrl());
					}
				}else{
					parentJson.accumulate("sub_button",childArray);
				}
				parentArray.put(parentJson);
			}
		}
		resultJson.accumulate("button", parentArray);
		return resultJson;
	}

	@Override
	public List<Tree> tree() {
		List<WeiMenuBO> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = weiMenuDao.find("select distinct t from WeiMenuBO t order by t.menuSeq");

		if ((l != null) && (l.size() > 0)) {
			for (WeiMenuBO r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getWeiMenuBO() != null) {
					tree.setPid(r.getWeiMenuBO().getId().toString());
				}
				tree.setText(r.getMenuName());
				lt.add(tree);
			}
		}
		return lt;
	}

}